package com.jinandi.app.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jinandi.app.dao.ResourceDao;
import com.jinandi.app.domain.Resource;

@Service
public class ResourceService extends BaseService {
	private ResourceDao resourceDao;

	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
	
	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * <查看角色拥有的资源>
	 * @param id
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:48:08
	 */
	@Transactional
	public List<Map<String,Object>> findList(Long id) {
		return jdbcTemplate.queryForList("SELECT res.id , res.url,(SELECT a.id FROM jnd_authorit a, jnd_authoritandres ar WHERE a.id = ? AND a.id = ar.authorit_id AND ar.res_id = res.id) AS aid FROM jnd_resource res", id);
	}
	
	/**
	 * <资源列表>
	 * @param params
	 * @param map
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:45:51
	 */
	@Transactional
	public Page<Resource> findResLisr(Map<String,Object> params,Map<String ,String> map) {
		Page<Resource> page= resourceDao.findAll(new Resource().getSpecification(params), getPageRequest(map));
		return page;
	}

	/**
	 * <根据ID删除资源>
	 * @param id
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:46:06
	 */
	@Transactional
	public void deleteResById(Long id) {
		resourceDao.deleteResById(id);
		
	}

	/**
	 * <新增，修改资源>
	 * @param resource
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:46:29
	 */
	@Transactional
	public void saveRes(Resource resource) {
		resourceDao.save(resource);
	}

	/**
	 * <根据ID查询资源>
	 * @param id
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:46:56
	 */
	@Transactional(readOnly = true)
	public Resource findResById(Long id) {
		return resourceDao.findResById(id);
	}
}
